package com.example.demo.Service.Impl;

import com.example.demo.Dao.ShopUserDao;
import com.example.demo.Entity.ShopUser;
import com.example.demo.Entity.UnlockApplication;
import com.example.demo.Service.EmailService;
import com.example.demo.Service.UserApplicationService;
import com.example.demo.Service.UserService;
import com.example.demo.Util.UserUtil;
import com.example.demo.Vo.Response;
import com.example.demo.Vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Pattern;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private ShopUserDao shopUserDao;
    @Autowired
    private EmailService emailService;
    @Autowired
    private UserApplicationService userApplicationService;

    @Override
    public ShopUser selectUserByUserEmail(String email) {
        return shopUserDao.selectUserByUserEmail(email);
    }

    @Override
    public ShopUser selectUserByUserID(String uid) {
        return shopUserDao.selectUserByUserID(uid);
    }

    @Override
    public boolean registerNewUser(UserVo userVo, HttpSession session) {
        if (emailService.compareVerificationCode(userVo.getVerificationCode(), userVo.getEmail(), session)) {
            ShopUser user = UserVo.ToUser(userVo);
            shopUserDao.registerNewUser(user);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void changePasswordByUserID(String uid, String password) {
        shopUserDao.changePasswordByUserID(uid, password);
    }

    @Override
    public void changeInfoByUserID(String uid, String name, String signature, String address, String pictureBase) {
        shopUserDao.changeInfoByUserID(uid, name, signature, address, pictureBase);
    }

    @Override
    public boolean findPasswordByUserEmail(String email, String verificationCode, String password, HttpSession session) {
        if (emailService.compareVerificationCode(verificationCode, email, session)) {
            String uid = selectUserByUserEmail(email).getUserID();
            shopUserDao.changePasswordByUserID(uid, password);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Response checkUserVar(UserVo userVo) {
        if (!UserUtil.checkUserID(userVo.getUid())) {
            return Response.createByErrorMessage("格式错误，用户名只能包含字母，数字与下划线，长度在3-16之间");
        } else if (!UserUtil.checkUserPassword(userVo.getPassword())) {
            return Response.createByErrorMessage("格式错误，密码必须包含大小写字母和数字的组合，可以使用特殊字符，长度在8-20之间");
        } else if (!UserUtil.checkUserEmail(userVo.getEmail())) {
            return Response.createByErrorMessage("请输入合法的email地址");
        } else if (!UserUtil.checkUserVerificationCode(userVo.getVerificationCode())) {
            return Response.createByErrorMessage("格式错误，验证码为6位数字");
        } else {
            return null;
        }
    }

    @Override
    public boolean checkUseAvailable(String uid) {
        ShopUser user = selectUserByUserID(uid);
        Date nowDate = new Date();
        if (user.getUnlockDate() != null && user.getUnlockDate().compareTo(nowDate) > 0) {
            return false;
        } else if (user.getUnlockDate() != null && user.getUnlockDate().compareTo(nowDate) <= 0) {
            shopUserDao.changeUnlockDateByUserID(uid, null);
            UnlockApplication unlockApplication = userApplicationService.selectUnlockApplicationByUserID(user.getUserID());
            if (unlockApplication != null) {
                userApplicationService.changeUnlockUsedByUserID(user.getUserID(), true);
            }
            return true;
        } else {
            UnlockApplication unlockApplication = userApplicationService.selectUnlockApplicationByUserID(user.getUserID());
            if (unlockApplication != null) {
                userApplicationService.changeUnlockUsedByUserID(user.getUserID(), true);
            }
            return true;
        }

    }

    @Override
    public void lockUserByUserID(String uid) {
        ShopUser user = selectUserByUserID(uid);
        Date lockDate = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(lockDate);
        calendar.add(calendar.SECOND, 30); //把日期往后增加一天,整数  往后推,负数往前移动
        lockDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
        shopUserDao.changeUnlockDateByUserID(uid, lockDate);
    }

    @Override
    public void unlockUserByUserID(String uid) {
        shopUserDao.changeUnlockDateByUserID(uid, null);
    }

    @Override
    public void changeAdminByUserID(String uid, boolean isAdmin) {
        shopUserDao.changeAdminByUserID(uid, isAdmin);
    }
}
